import {
  formatTimeDate
} from '../../../../utils/formatData';
import Utils from "../../../../utils/utils";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    luckyBeansInfo: {}, // 幸运豆信息
    billList: [], // 幸运豆账单列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
    pea_estimate: 0,
    coverAllMoney: 0,
    dateText: formatTimeDate(new Date(), 'yyyy-MM'),
    dataTimeType: 'year-month',
    currentDate: new Date().getTime(),
    currentSelectDate: new Date().getTime(),
    selectTimeRadio: 'month',
    isShowSelectTime: false,
    ServiceText: false,
    formatter(type, value) {
      switch (type) {
        case 'year':
          return `${value}年`;
        case 'month':
          return `${value}月`;
        case 'day':
          return `${value}日`;
        default:
          return value;
      }
    }
  },
  initPage() {
    let self = this;
    self.getUserInfo(function(userInfo) {
      let coverAllMoney;
      self.coverRatio = userInfo.common.pea_rate;
      self.getData()
    });
  },
  coverMoney: function(money) {
    money = Utils.fixFloat(money / this.coverRatio);
    this.setData({
      coverAllMoney: money,
    });
  },
  // 打开时间选择
  onOpenSelectTime() {
    this.setData({
      isShowSelectTime: true
    })
  },
  // 时间选择 - 取消
  onCancelTime() {
    this.setData({
      isShowSelectTime: false,
      currentDate: this.data.currentSelectDate
    })
  },
  // 时间选择 - 确定
  onConfirmTime() {
    const {
      selectTimeRadio,
      currentDate
    } = this.data;
    let dateText = '';
    if (selectTimeRadio === 'month') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM')
    } else if (selectTimeRadio === 'day') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM-dd')
    }
    this.setData({
      isShowSelectTime: false,
      currentSelectDate: currentDate,
      dateText: dateText,
      page: 1
    });
    this.getData()
  },

  // 修改时间类型选择单选框
  onChangeTimeRadio(event) {
    let dateType = '';
    let radioType = event.detail;
    if (radioType === 'month') {
      dateType = 'year-month'
    } else if (radioType === 'day') {
      dateType = 'date'
    }
    this.setData({
      selectTimeRadio: radioType,
      dataTimeType: dateType
    });
  },

  // 时间选择器 - 当值变化时触发的事假
  onInputPicker(event) {
    this.setData({
      currentDate: event.detail
    });
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {
    this.setData({
      page: 1
    });
    this.getData()
  },

  // 页面上拉触底事件的处理函数
  onReachBottom: function() {
    this.setData({
      page: this.data.page + 1
    });
    this.getData()
  },
  // 获取幸运豆明细
  getData() {
    const {
      dateText,
      selectTimeRadio,
      page,
      pageSize,
      billList
    } = this.data;
    let requestData = {};
    let self = this;
    if (selectTimeRadio === 'month') {
      requestData = {
        mouth: dateText,
        page: page,
        limit: pageSize
      }
    } else if (selectTimeRadio === 'day') {
      requestData = {
        day: dateText,
        page: page,
        limit: pageSize
      }
    }
    requestData['mouth'] = '';
    Utils.request({
      url: '/s/userinfo/PeaEstimateDetail',
      data: requestData,
      loading: true,
      callback: function(result) {
        wx.stopPullDownRefresh();
        if (result.status === 'ok') {
          self.coverMoney(result.res.pea_estimate);
          // result.res.list = [
          //   {
          //     "id":12312,
          //     "count":1231,				//数量  总是正的
          //     "info":"添加到我的小程序",	//描述
          //     "tm":1231231,			//时间 Unix 时间戳
          //     "tips":"7天后到账",			//到账时间描述
          //     "stat":1,					//状态 0未到账 2拒绝 3失效
          //   }
          // ];
          self.setData({
            pea_estimate: result.res.pea_estimate,
            totalNum: result.res.totalcount,
            billList: page === 1 ? result.res.list : billList.concat(result.res.list)
          });
        }
      }
    });
  },
  closePopup() {
    this.setData({ ServiceText:false})

  }
});